<template>
	<view class="page">
		<view class="add-list">
			<view class="list">
				<view class="title">
					<text>姓名</text>
					<text class="star">*</text>
				</view>
				<view class="content">
					<input type="text" v-model="name" placeholder="请输入姓名">
				</view>
			</view>
			<view class="list">
				<view class="title">
					<text>身份证</text>
				</view>
				<view class="content">
					<input type="text" v-model="code" placeholder="请输入身份证号码">
				</view>
			</view>
		</view>
		<!-- 保存按钮 -->
		<view class="save-btn">
			<view class="btn" @click="onSave">{{type==0?'保存':'修改'}}</view>
		</view>
		<!-- <DialogBox ref="DialogBox"></DialogBox> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name:'',
				code:'',
				id:null,
				type:0,
			};
		},
		onLoad(options){
			uni.setNavigationBarColor({
				frontColor:"#ffffff",
				backgroundColor:"#ae87d8"
			})
			if(options.type){
				this.type = options.type
				let pars = JSON.parse(decodeURIComponent(options.item))
				this.id = pars.id
				this.name = pars.uname
				this.code = pars.idcard
				uni.setNavigationBarTitle({
					title:'编辑报关人信息'
				})
			}
		},
		methods:{
			/**
			 * 保存点击
			 */
			onSave(){
				if(!this.name){
					uni.showToast({
						title: '请输入姓名',
						icon: 'none',
					})
					return false
				}
				if(!uni.$u.test.idCard(this.code)){
					uni.showToast({
						title: '请输入正确的身份证号码',
						icon: 'none',
					})
					return false
				}
				if(this.type==2){
					//编辑修改
					this.$u.api.idcardEdit({
						id:this.id,
						uname:this.name,
						idcard:this.code
					}).then(res=>{
						if(res.status){
							uni.showToast({
								title: '修改成功',
								icon: 'success',
							}).then(()=>{
								uni.navigateBack();
							})
						}
						this.isNone = true
					})
					.catch(err=>{
						console.log(err);
						uni.showToast({
							icon: 'none',
							title: "网络异常，请稍后"
						})
					})
				}else{
					this.$u.api.idcardAdd({
						uname:this.name,
						idcard:this.code
					}).then(res=>{
						if(res.status){
							uni.showToast({
								title: '保存成功',
								icon: 'success',
							}).then(()=>{
								uni.navigateBack();
							})
						}
						this.isNone = true
					})
					.catch(err=>{
						console.log(err);
						uni.showToast({
							icon: 'none',
							title: "网络异常，请稍后"
						})
					})
				}
				
				
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'InvoiceAdd.scss';
</style>
